//
//  SwitchView.m
//  Wallpaper
//
//  Created by ytkjs2 on 2019/9/25.
//  Copyright © 2019 ytk. All rights reserved.
//

#import "SwitchView.h"
@interface SwitchView()
{
    UIButton * button[8];
}
@property (nonatomic, strong)NSArray * titleArray;

@property (nonatomic, strong)UILabel * line;

@end


@implementation SwitchView
- (NSArray *)titleArray
{
    if (!_titleArray) {
        _titleArray = [NSArray array];
    }
    return _titleArray;
}

- (instancetype)initWithFrame:(CGRect)frame withTitleArray:(NSArray *)arr
{
    if (self = [super initWithFrame:frame]) {
        float btnWidth = frame.size.width/arr.count;
        float btnX = 0;
        self.titleArray = arr;
        for (int i = 0; i < arr.count; i ++) {
            button[i] = [UIButton buttonWithType:UIButtonTypeCustom];
            CGSize size = [arr[i] boundingRectWithSize:CGSizeMake(100, 30) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:16]} context:nil].size;
            [button[i] setFrame:CGRectMake(0, 0, size.width, 30)];
            if (i != 0) {
                btnX = CGRectGetMaxX(button[i - 1].frame) + 5;
            }
            button[i].center = CGPointMake(btnX + size.width/2, 25);
            [button[i] setTitle:arr[i] forState:UIControlStateNormal];
            [button[i] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//            [button[i] setTitleColor:APP_THEME_COLOR forState:UIControlStateSelected];
            [button[i].titleLabel setFont:[UIFont systemFontOfSize:13]];
            button[i].tag = i;
            [button[i] addTarget:self action:@selector(typeSelectChange:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:button[i]];
        }
//        button[0].selected = YES;
        [button[0].titleLabel setFont:[UIFont boldSystemFontOfSize:16]];
        
        _line = [[UILabel alloc]initWithFrame:CGRectMake((btnWidth - 15)/2, 37, 15, 3)];
        _line.center = CGPointMake(CGRectGetMidX(button[0].frame), 39);
        [_line.layer setMasksToBounds:YES];
        [_line.layer setCornerRadius:1.5];
        [_line setBackgroundColor:[UIColor whiteColor]];
        [self addSubview:_line];
    }
    return self;
}

- (void)typeSelectChange:(UIButton *)btn
{
    [self scrollButtons:btn.tag];
    
    [self.controller performSelector:@selector(typeSelectChange:) withObject:btn];
}

- (void)scrollButtons:(NSInteger)index
{
    for (int i = 0; i < _titleArray.count; i ++) {
        [button[i].titleLabel setFont:[UIFont systemFontOfSize:13]];
        if (index == i) {
            [button[i].titleLabel setFont:[UIFont boldSystemFontOfSize:16]];
        }
    }
    __weak typeof(self)weakSelf = self;
    [UIView animateWithDuration:0.2f animations:^{
        CGSize size = [weakSelf.titleArray[index] boundingRectWithSize:CGSizeMake(100, 30) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:16]} context:nil].size;
        [weakSelf.line setFrame:CGRectMake(0, 0, size.width/2, 3)];
        weakSelf.line.center = CGPointMake(CGRectGetMidX(button[index].frame), 37);
    }];
}

@end
